package com.wdh.kjs.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.R;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.JsonModel_my;
import com.wdh.kjs.util.EditTextLengthUtil;
import com.wdh.kjs.util.ToastUtils;

import java.math.BigDecimal;

/**
 * Created by feifei on 2016/4/21.
 */
public class UpdateLoginPasswordActivity extends BaseTopActivity implements View.OnClickListener {
    private EditText et_ulp_oldPassWord;
    private EditText et_ulp_newPassWord;
    private EditText et_ulp_reNewPassWord;
    private TextView updateLoginPassword_forget;
    private ImageView updateLoginPassword_return;
    private Button bt_ulp_commit;
    private Button bt_ulp_reInput;
    private JsonModel_my jsonModel_my;
    private String phoneNumber;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_login_password);
        SharedPreferences share = getSharedPreferences("logininfo", MODE_PRIVATE);
        phoneNumber = share.getString("phoneNumber", "");
        initview();
    }

    private void loadData() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("phoneNumber", phoneNumber);
        params.addBodyParameter("oldpw", et_ulp_oldPassWord.getText().toString());
        params.addBodyParameter("newpw", et_ulp_newPassWord.getText().toString());

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_UPDATE_LOGINPW, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
                jsonModel_my = gson.fromJson(responseInfo.result, JsonModel_my.class);
                if (jsonModel_my.getReturnMsg().equals("success")) {
                    trueDialogShow("密码修改已成功！");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            intent = new Intent();
                            finish();
                            intent.setClass(UpdateLoginPasswordActivity.this, SecuritySetting.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                        }
                    }, 2000);
                } else if (jsonModel_my.getReturnMsg().equals("wrongoldpassword")) {
                    falseDialogShow("原密码错误！");
                } else if (jsonModel_my.getReturnMsg().equals("nofind")) {
                    falseDialogShow("找不到用户！");
                } else if (jsonModel_my.getReturnMsg().equals("wrong")) {
                    falseDialogShow("密码修改失败！");
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                ToastUtils.showShort(getBaseContext(), "网络异常！");
            }
        });

    }

    private void initview() {
        et_ulp_oldPassWord = (EditText) findViewById(R.id.et_ulp_oldPassWord);
        et_ulp_newPassWord = (EditText) findViewById(R.id.et_ulp_newPassWord);
        et_ulp_reNewPassWord = (EditText) findViewById(R.id.et_ulp_reNewPassWord);
        updateLoginPassword_forget = (TextView) findViewById(R.id.updateLoginPassword_forget);
        updateLoginPassword_return = (ImageView) findViewById(R.id.updateLoginPassword_return);
        bt_ulp_commit = (Button) findViewById(R.id.bt_ulp_commit);
        bt_ulp_reInput = (Button) findViewById(R.id.bt_ulp_reInput);
        bt_ulp_commit.setOnClickListener(this);
        bt_ulp_reInput.setOnClickListener(this);
        updateLoginPassword_forget.setOnClickListener(this);
        updateLoginPassword_return.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_ulp_reInput:
                et_ulp_oldPassWord.setText("");
                et_ulp_newPassWord.setText("");
                et_ulp_reNewPassWord.setText("");
                break;
            case R.id.bt_ulp_commit:
                int oldPwdLength = EditTextLengthUtil.check_length(et_ulp_oldPassWord.getText().toString());
                int newPwdLength = EditTextLengthUtil.check_length(et_ulp_newPassWord.getText().toString());
                if (et_ulp_oldPassWord.getText().toString().equals("")) {
                    falseDialogShow("请输入原密码！");
                } else if (oldPwdLength != 0) {
                    if (oldPwdLength > 0) {
                        falseDialogShow("密码位数多于" + EditTextLengthUtil.max_length + "位");
                    } else {
                        falseDialogShow("密码位数少于" + EditTextLengthUtil.min_length + "位");
                    }
                } else if (et_ulp_newPassWord.getText().toString().equals("")) {
                    falseDialogShow("请输入新密码！");
                } else if (newPwdLength != 0) {
                    if (newPwdLength > 0) {
                        falseDialogShow("新密码位数多于" + EditTextLengthUtil.max_length + "位");
                    } else {
                        falseDialogShow("新密码位数少于" + EditTextLengthUtil.min_length + "位");
                    }
                } else if (et_ulp_reNewPassWord.getText().toString().equals("")) {
                    falseDialogShow("请输入确认密码！");
                } else if (!et_ulp_newPassWord.getText().toString().equals(et_ulp_reNewPassWord.getText().toString())) {
                    falseDialogShow("新密码两次输入不一致！");
                } else {
                    loadData();
                }
                break;
            case R.id.updateLoginPassword_forget:
                intent = new Intent(this, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.updateLoginPassword_return:
                finish();
                break;
            default:
                break;
        }
    }

    private void falseDialogShow(String hint) {
        View dialog = (LinearLayout) getLayoutInflater().inflate(R.layout.view_dialog_false, null);
        Button bt_dialog_false_negative = (Button) dialog.findViewById(R.id.bt_dialog_false_negative);
        Button bt_dialog_false_positive = (Button) dialog.findViewById(R.id.bt_dialog_false_positive);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_false_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();
        bt_dialog_false_negative.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
            }
        });
        bt_dialog_false_positive.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
                et_ulp_oldPassWord.setText("");
                et_ulp_newPassWord.setText("");
                et_ulp_reNewPassWord.setText("");
            }
        });
    }

    private void trueDialogShow(String hint) {
        View dialog = (LinearLayout) getLayoutInflater().inflate(R.layout.view_dialog_true, null);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_true_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();
    }

}
